<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2024/8/9
  Time: 10:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加电影页面</title>
    <script src="${pageContext.request.contextPath}/statics/jquery-1.8.3.min.js"></script>
    <style>
        table,td,th{
            border: 1px solid black;
            background-color: #e5dd9b;
        }
    </style>
</head>
<body>
    <h1>添加电影信息</h1>
    <form method="post" action="${pageContext.request.contextPath}/add">
        <table>
            <tr>
                <td>电影名称：</td>
                <td><input type="text" name="movieName" id="name"><span id="movieNameStr"></span></td>
            </tr>
            <tr>
                <td>电影类型：</td>
                <td><input type="text" name="movieType"><span id="movieTypeStr"></span></td>
            </tr>
            <tr>
                <td>电影价格：</td>
                <td><input type="text" name="price"><span id="priceStr"></span></td>
            </tr>
            <tr>
                <td>是否推荐：</td>
                <td><input type="radio" name="recommend" value="1" checked>推荐&nbsp;&nbsp;<input type="radio" name="recommend" value="0">不推荐</td>
            </tr>
            <tr>
                <td>上映时间：</td>
                <td><input type="text" name="startTime"><strong></strong>格式:yyyy-MM-dd<span id="startTimeStr"></span></td>
            </tr>
            <tr>
                <td>下映时间：</td>
                <td><input type="text" name="endTime"><strong>格式:yyyy-MM-dd</strong><span id="endTimeStr"></span></td>
            </tr>
            <tr>
                <td>投票数：</td>
                <td><input type="text" name="votes"><span id="votesStr"></span></td>
            </tr>
            <tr>
                <td>操作：</td>
                <td><input type="submit" value="添加">&nbsp;&nbsp;<input type="button" onclick="javascript:location.href='toHome'" value="返回"></td>
            </tr>
        </table>
    </form>
    <script>
        $(function (){
            $("#name").blur(function (){
                let value = $(this).val();
                $.ajax({
                    url:"MovieCheckName/" + value,
                    dataType:"text",
                    type:"GET",
                    success:function (res){
                        let movieStr = $("#movieNameStr");
                        movieStr.html("");
                        if (res == "true"){
                            movieStr.html("该电影已经存在了！");
                            movieStr.css("color","red");
                        }else{
                            movieStr.html("");
                        }
                    }
                })
            });
        })
        $("form").submit(function (){
            var flag = true;
            if (!checkMovieName()) {
                flag = false;
            }
            if (!checkMovieType()) {
                flag = false;
            }
            if (!checkPrice()) {
                flag = false;
            }
            if (!checkStartTime()) {
                flag = false;
            }
            if (!checkEndTime()) {
                flag = false;
            }
            if (!checkVotes()){
                flag = false;
            }
            return flag;
        });
        function checkMovieName(){
            var name = $("input[name='movieName']").val();
            var name_msg = $("#movieNameStr");
            name_msg.html("");
            if(name == ""){
                name_msg.html("电影名称不能为空！");
                return false;
            }
            return true;
        }
        function checkMovieType(){
            var type = $("input[name='movieType']").val();
            var type_msg = $("#movieTypeStr");
            type_msg.html("");
            if(type == ""){
                type_msg.html("电影类型不能为空！");
                return false;
            }
            return true;
        }
        function checkPrice(){
            var price = $("input[name='price']").val();
            var price_msg = $("#priceStr");
            price_msg.html("");
            if(price == ""){
                price_msg.html("电影价格不能为空！");
                return false;
            }
            return true;
        }
        function checkStartTime(){
            var startTime = $("input[name='startTime']").val();
            var startTime_msg = $("#startTimeStr");
            var regstartTime = /\d{4}-\d{2}-\d{2}/;
            startTime_msg.html("");
            if(startTime == ""){
                startTime_msg.html("上映时间不能为空！");
                return false;
            }else if (!regstartTime.test(startTime)){
                startTime_msg.html("时间格式不正确");
                return false;
            }
            return true;
        }
        function checkEndTime(){
            var endTime = $("input[name='endTime']").val();
            var endTime_msg = $("#endTimeStr");
            var regendTime = /\d{4}-\d{2}-\d{2}/;
            endTime_msg.html("");
            if(endTime == ""){
                endTime_msg.html("下映时间不能为空！");
                return false;
            }else if (!regendTime.test(endTime)){
                endTime_msg.html("时间格式不正确");
                return false;
            }
            return true;
        }
        function checkVotes(){
            var votes = $("input[name='votes']").val();
            var votes_msg = $("#votesStr");
            votes_msg.html("");
            if(votes == ""){
                votes_msg.html("投票数不能为空！");
                return false;
            }
            return true;
        }
    </script>
</body>
</html>
